home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / ainet / t2runtim.c < prev    next >
C/C++ Source or Header  |  1997-07-11  |  13KB  |  356 lines

  1. /* ----------------------------------------------------------------------- *
  2.  *                                                                         *
  3.  *    (C) Copyright 1996 by:  aiNet                                        *
  4.  *                                        Trubarjeva 42, SI-3000 Celje                 *
  5.  *                                        Europe, Slovenia                             *
  6.  *     All Rights Reserved                                                 *
  7.  *                                                                         *
  8.  *     Subject: C code for single vector prediction.                       *
  9.  *     File:    t2runtim - The XOR problem with dynamic model creation     *
  10.  *                                                                         *
  11.  * ----------------------------------------------------------------------- */
  12.  
  13. /*--------------------------------------------------------------------------
  14.     Here it will be shown how we can solve the XOR problem using
  15.     aiNet C functions. The problem is the same as in DLLTST02.
  16.  
  17.     The XOR problem:
  18.     ================
  19.         Number of model vectors: 4
  20.              Number of variables: 3
  21.      Number of input variables: 3
  22.          Any discrete variables: NONE
  23.  
  24.         Model vectors:  Inp,Inp,Out
  25.                   row 1:  1,  1,  0
  26.                   row 2:  1,  0,  1
  27.                   row 3:  0,  1,  1
  28.                   row 4:  0,  0,  0
  29.  
  30.     Test vectors (vectors which will be used in prediction) together with
  31.     penalty coefficient and penalty method.
  32.  
  33.          Prediction vectors:  Inp  Inp  Out
  34.                           prd 1:  0.9  0.1  ??
  35.                           prd 2:  0.1  0.9  ??
  36.                           prd 3:  0.2  0.2  ??
  37.                           prd 4:  0.7  0.7  ??
  38.  
  39.          Penalty coeffcient: 1.5
  40.          Penalty methods:    NEAREST
  41.  
  42.     NOTE: Selected penalty coefficients are in no case optimal.
  43.             They were selected randomly, to perform a few tests.
  44.             The test results were compared with the results calculated by
  45.             the main aiNet 1.14 application.
  46.  
  47.     --------------------------------------------------------------------------
  48.     Results (rounded at fourth decimal):
  49.     --------------------------------------------------------------------------
  50.  
  51.          Penalty cefficient: 1.5
  52.          Penalty method:     NEAREST N.
  53.                                                   (RESULT)
  54.          Prediction vectors:  Inp  Inp  (  Out )
  55.                           prd 1:  0.9  0.1  (0.9989)
  56.                           prd 2:  0.1  0.9  (0.9989)
  57.                           prd 3:  0.2  0.2  (0.1054)
  58.                           prd 4:  0.7  0.7  (0.3449)
  59.  
  60.     -------------------------------------------------------------------------*/
  61.  
  62.    /*
  63.     * This file assumes that ainetxx.dll will be loaded at run time,
  64.     * which is default option and no flags need to be defined.
  65.     * ainetxx.lib must NOT be included in the linking process.
  66.     */
  67.  
  68. #include "ainetdll.h"
  69. #include <stdio.h>
  70. #include <stdlib.h>
  71.  
  72. /*
  73.  * Path and the filename of dll which will be loaded.
  74.  */
  75.  
  76. #if defined(__WIN32__)
  77. const char ainetDll[] = "ainet32.dll";
  78. #else
  79. const char ainetDll[] = "ainet16.dll";
  80. #endif
  81. /*
  82.  * Pointers to ainet dll functions. They are made global - all functions
  83.  * can use them.
  84.  */
  85.  
  86. t_aiRegistration              aiRegistration;
  87. t_aiGetVersion                aiGetVersion;
  88. t_aiCreateModel               aiCreateModel;
  89. t_aiCreateModelFromCSVFile    aiCreateModelFromCSVFile;
  90. t_aiDeleteModel               aiDeleteModel;
  91. t_aiNormalize                 aiNormalize;
  92. t_aiDenormalize               aiDenormalize;
  93. t_aiPrediction                aiPrediction;
  94. t_aiGetNumberOfVariables      aiGetNumberOfVariables;
  95. t_aiGetNumberOfModelVectors   aiGetNumberOfModelVectors;
  96. t_aiGetNumberOfInputVariables aiGetNumberOfInputVariables;
  97. t_aiSetDiscreteFlag           aiSetDiscreteFlag;
  98. t_aiGetDiscreteFlag           aiGetDiscreteFlag;
  99. t_aiSetVariable               aiSetVariable;
  100. t_aiGetVariable               aiGetVariable;
  101. t_aiGetVariableVB             aiGetVariableVB;
  102. t_aiGetCSVFileModelSize       aiGetCSVFileModelSize;
  103. // New in version 1.24
  104. t_aiSetCapacity                    aiSetCapacity;
  105. t_aiGetCapacity                    aiGetCapacity;
  106. t_aiGetFreeEntries                aiGetFreeEntries;
  107. t_aiInsertModelVector            aiInsertModelVector;
  108. t_aiOverwriteModelVector        aiOverwriteModelVector;
  109. t_aiAppendModelVector            aiAppendModelVector;
  110. t_aiDeleteModelVector            aiDeleteModelVector;
  111. t_aiPredictionEx                    aiPredictionEx;
  112. t_aiExcludeModelVector            aiExcludeModelVector;
  113. t_aiExcludeModelVectorRange    aiExcludeModelVectorRange;
  114. t_aiIsModelVectorExcluded        aiIsModelVectorExcluded;
  115. t_aiSaveCSVFile                    aiSaveCSVFile;
  116.  
  117. /*
  118.  *  ainet32.dll module variable.
  119.  */
  120.  
  121. HINSTANCE hLib;
  122.  
  123. /*
  124.  *  The load_aiNetLibrary() function is implemented below.
  125.  *  This function will load ainet32.dll and define pointers to
  126.  *  ainet functions.
  127.  */
  128.  
  129. int load_aiNetLibrary(void);
  130.  
  131. /*
  132.  *
  133.  */
  134.  
  135. void main()
  136. {
  137.  
  138.    /*
  139.     * Working variables
  140.     */
  141.  
  142.     int i;
  143.     int version;
  144.  
  145.     /*
  146.      * vectors to be predicted
  147.      */
  148.  
  149.     float predict[4][3] = { { 0.9,0.1, 999 },
  150.                                     { 0.1,0.9, 999 },
  151.                                     { 0.2,0.2, 999 },
  152.                                     { 0.7,0.7, 999 } };
  153.     /*
  154.      * Model variable
  155.      */
  156.  
  157.     aiModel* model;
  158.  
  159.    /*
  160.     * Load DLL
  161.     */
  162.    if( !load_aiNetLibrary() ) {
  163.         exit(EXIT_FAILURE);
  164.     }
  165.  
  166.     /*
  167.      * Title
  168.      */
  169.  
  170.     version = aiGetVersion();
  171.     printf( "\naiNetDLL version %i.%i! (C) Copyright by aiNet, 1996",
  172.               version/100, version%100 );
  173.     printf( "\n---------------------------------------------------\n" );
  174.  
  175.     /*
  176.      * Register DLL
  177.      */
  178.  
  179.     aiRegistration( "Your registration name", "Your code" );
  180.  
  181.     /*
  182.      * Allocate the model variable and necessary memory
  183.      */
  184.  
  185.     model = aiCreateModel(4,   /* 4 model vectors   */
  186.                                  3,   /* 3 variables       */
  187.                                    2);  /* 2 input variables */
  188.     if(!model) {
  189.         printf( "\nError: Something went wrong during model creation!" );
  190.         exit(EXIT_FAILURE);
  191.     }
  192.  
  193.     /*
  194.      * Loading data into the model using aiSetVariable function
  195.      */
  196.  
  197.     aiSetVariable(model,1,1, 1.0); /* first model vector */
  198.     aiSetVariable(model,1,2, 1.0); /* 1 xor 1 = 0        */
  199.     aiSetVariable(model,1,3, 0.0);
  200.  
  201.     aiSetVariable(model,2,1, 1.0); /* second model vector */
  202.     aiSetVariable(model,2,2, 0.0); /* 1 xor 0 = 1         */
  203.     aiSetVariable(model,2,3, 1.0);
  204.  
  205.     aiSetVariable(model,3,1, 0.0); /* third model vector */
  206.     aiSetVariable(model,3,2, 1.0); /* 0 xor 1 = 1        */
  207.     aiSetVariable(model,3,3, 1.0);
  208.  
  209.     aiSetVariable(model,4,1, 0.0); /* fourth model vector */
  210.     aiSetVariable(model,4,2, 0.0); /* 0 xor 0 = 0         */
  211.     aiSetVariable(model,4,3, 0.0);
  212.  
  213.     /*
  214.      * Output the model
  215.      */
  216.  
  217.     printf( "\n             Model name: aiNet DLL test 2 (aiCreateModel)" );
  218.     printf( "\nNumber of model vectors: %i", aiGetNumberOfModelVectors(model));
  219.     printf( "\n    Number of variables: %i", aiGetNumberOfVariables(model));
  220.     printf( "\n         Variable names: A,   B,   A xor B" );
  221.     printf( "\n          Discrete flag: %i,   %i,   %i",
  222.               aiGetDiscreteFlag(model,1),
  223.               aiGetDiscreteFlag(model,2),
  224.               aiGetDiscreteFlag(model,3) );
  225.     for( i=1; i<=aiGetNumberOfModelVectors(model); i++ ) {
  226.         printf( "\n\t\t\t %3.1lf, %3.1lf, %3.1lf",
  227.                   aiGetVariable(model, i,1),
  228.                   aiGetVariable(model, i,2),
  229.                   aiGetVariable(model, i,3) );
  230.     }
  231.  
  232.     /*
  233.      * Normalize the model
  234.      */
  235.  
  236.     aiNormalize(model, NORMALIZE_REGULAR);
  237.  
  238.     /*
  239.      * Prediction
  240.      * This test has nearest neighbour penalty coefficient 1.50
  241.      */
  242.  
  243.     printf( "\n\n  Penalty coefficient: 1.50" );
  244.     printf(   "\n       Penalty method: NEAREST N." );
  245.     printf(   "\n\t A(inp), B(inp), A xor B(out)" );
  246.     for ( i=0; i<4; i++ ) {
  247.         aiPrediction(model, predict[i], 1.50, PENALTY_NEAREST);
  248.         printf( "\n\t%7.4lf, %7.4lf, %7.4lf",
  249.                   predict[i][0],predict[i][1],predict[i][2] );
  250.     }
  251.  
  252.     /*
  253.      * Denormalize the model (in this case it is not necessary)
  254.      */
  255.  
  256.     aiDenormalize(model);
  257.  
  258.     /*
  259.      * Free alocated memory
  260.      */
  261.  
  262.     aiDeleteModel(model);
  263.    FreeLibrary(hLib);
  264.  
  265.     printf( "\n\nEnd." );
  266.     exit(EXIT_SUCCESS);
  267. }
  268.  
  269. int load_aiNetLibrary()
  270. {
  271.    /*
  272.     * Load the Dynamic Link Library AINET32.DLL
  273.     */
  274.  
  275.    hLib = LoadLibrary(ainetDll);
  276.    if((unsigned)hLib<=HINSTANCE_ERROR){
  277.       char bfr[40];
  278.       wsprintf(bfr, "Failure loading library: %s", ainetDll);
  279.       MessageBox(NULL, bfr, "Error", MB_OK|MB_APPLMODAL);
  280.       return 0;
  281.    }
  282.  
  283.    /*
  284.     *